package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.domain.OaWorktime;
import com.hundredapp.oa.repository.ClassmngManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.repository.SubjectManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.repository.WorktimeManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.ClassVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Class1 extends ActionSupport {

	@Autowired
	private ClassmngManager classmng;
	@Autowired
	private UserManager usermng;
	@Autowired
	private StudentManager studentmng;
	@Autowired
	private WorktimeManager worktimemanager;
	@Autowired
	private SubjectManager subjectmng;

	private Object success;
	private String msg;
	private int bishopid;
	private int assistantid;
	private OaClass oaclass;
	private int id;
	private List<ClassVO> root;
	private List<OaStudent> students;
	private List<OaWorktime> worktimes;
	private OaClass clazz;
	private String studentlist;
	private String classname;
	private int limit;
	private int start;
	private String worktimes_str;
	private int oaUserByBishopid;
	private int oaUserByAssistantid;
	private int worktimeid;
	private int subjectid;

	public String add() {
		oaclass.setOaUserByAssistant(usermng.findUserById(assistantid));
		oaclass.setOaUserByBishop(usermng.findUserById(bishopid));
		oaclass.setOaSubject(subjectmng.findById(subjectid));
		classmng.add(oaclass);
		for(String str:worktimes_str.split(",")){
			OaWorktime wt=new OaWorktime();
			wt.setCreateUserId(WebUtil.GetLoginUserId());
			wt.setWorktime(str);
			wt.setOaClass(oaclass);
			worktimemanager.add(wt);
		}
		success = true;
		msg = "添加成功";

		return SUCCESS;
	}
	
	public String update() {
		OaClass clazz=classmng.findById(id);
		System.out.println("-------"+id);
		clazz.setAge(oaclass.getAge());
		clazz.setCount(oaclass.getCount());
		clazz.setClassName(oaclass.getClassName());
		clazz.setOaUserByAssistant(usermng.findUserById(assistantid));
		clazz.setOaUserByBishop(usermng.findUserById(bishopid));
		clazz.setClassroom(oaclass.getClassroom());
		clazz.setOaSubject(subjectmng.findById(subjectid));
		classmng.update(clazz);
//		worktimemanager.deletebyclass(id);
//		for(String str:worktimes_str.split(",")){
//			OaWorktime wt=new OaWorktime();
//			wt.setCreateUserId(WebUtil.GetLoginUserId());
//			wt.setWorktime(str);
//			wt.setOaClass(clazz);
//			worktimemanager.add(wt);
//		}
		success = true;
		msg = "修改成功";

		return SUCCESS;
	}
	
	public String find(){
		clazz=classmng.findById(id);
		oaUserByBishopid=clazz.getOaUserByBishop().getId();
		oaUserByAssistantid=clazz.getOaUserByAssistant().getId();
		return SUCCESS;
	}

	public String all() {
		root = new ArrayList<ClassVO>();
		String condition = SelectType.SELECT_ALL+" and state = 1";//只查询未结业的班级
		if(classname!=null){
			classname=JSEscape.unescape(classname);
			condition+=" and className like '%"+classname+"%'";
		}
		for (OaClass oc : classmng.findAll(condition,start,limit)) {
			ClassVO temp = new ClassVO();
			temp.setId(oc.getId());
			temp.setAge(oc.getAge());
			temp.setAssistantName(oc.getOaUserByAssistant().getDisplayName());
			temp.setBishopName(oc.getOaUserByBishop().getDisplayName());
			temp.setClassName(oc.getClassName());
			temp.setCount(oc.getCount());
			temp.setSubject(oc.getOaSubject().getDisplayName());
			temp.setClassroom(oc.getClassroom());
			root.add(temp);
		}
		return SUCCESS;
	}
	
	public String findbyuser() {
		root = new ArrayList<ClassVO>();
		for (OaClass oc : classmng.findAll("(bishop="+WebUtil.GetLoginUserId()+" or assistant="+WebUtil.GetLoginUserId()+") and state = 1")) {
			ClassVO temp = new ClassVO();
			temp.setId(oc.getId());
			temp.setAge(oc.getAge());
			temp.setAssistantName(oc.getOaUserByAssistant().getDisplayName());
			temp.setBishopName(oc.getOaUserByBishop().getDisplayName());
			temp.setClassName(oc.getClassName());
			temp.setCount(oc.getCount());
			root.add(temp);
		}
		return SUCCESS;
	}
	
	public String findselected(){
		students=classmng.findStudent(id);
		return SUCCESS;
	}
	
	public String findunselected(){
		students=classmng.findUnselectStudent(id);
		return SUCCESS;
	}
	
	public String addstudents(){
		OaClass clazz=classmng.findById(id);
		for(String studentid:studentlist.split(",")){
			clazz.getOaStudents().add(studentmng.findById(Integer.parseInt(studentid)));
		}
		classmng.update(clazz);
		return SUCCESS;
	}
	
	public String deletestudents(){
		OaClass clazz=classmng.findById(id);
		for(String studentid:studentlist.split(",")){
			clazz.getOaStudents().remove(studentmng.findById(Integer.parseInt(studentid)));
		}
		classmng.update(clazz);
		return SUCCESS;
	}
	
	public String addworktime(){
		OaWorktime wt=new OaWorktime();
		wt.setCreateUserId(WebUtil.GetLoginUserId());
		wt.setWorktime(worktimes_str);
		wt.setOaClass(classmng.findById(id));
		worktimemanager.add(wt);
		return SUCCESS;
	}
	
	public String deleteworktime(){
		OaWorktime wt=worktimemanager.findWorktime(String.format("oaClass.id=%s and id=%s",id,worktimeid));
		worktimemanager.delete(wt.getId());
		return SUCCESS;
	}

	public String delete() {
		try {
			classmng.delete(id);
			success = true;
			msg = "添加成功";
		} catch (Exception e) {
			success = false;
			msg = "添加失败";
		}
		return SUCCESS;
	}
	
	public String findworktime() {
		worktimes = worktimemanager.findWorktimes("oaClass.id="+id, start, limit);
		return SUCCESS;
	}
	
	public void setClassmng(ClassmngManager classmng) {
		this.classmng = classmng;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setBishopid(int bishopid) {
		this.bishopid = bishopid;
	}

	public void setAssistantid(int assistantid) {
		this.assistantid = assistantid;
	}

	public void setUsermng(UserManager usermng) {
		this.usermng = usermng;
	}

	public void setOaclass(OaClass oaclass) {
		this.oaclass = oaclass;
	}

	public List<ClassVO> getRoot() {
		return root;
	}

	public void setId(int id) {
		this.id = id;
	}

	public OaClass getOaclass() {
		return oaclass;
	}

	public List<OaStudent> getStudents() {
		return students;
	}

	public String getStudentlist() {
		return studentlist;
	}

	public void setStudentlist(String studentlist) {
		this.studentlist = studentlist;
	}

	public void setStudentmng(StudentManager studentmng) {
		this.studentmng = studentmng;
	}

	public OaClass getClazz() {
		return clazz;
	}

	public void setClassname(String classname) {
		this.classname = classname;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setStart(int start) {
		this.start = start;
	}
	public List<OaWorktime> getWorktimes() {
		return worktimes;
	}

	public void setWorktimes(List<OaWorktime> worktimes) {
		this.worktimes = worktimes;
	}

	public String getWorktimes_str() {
		return worktimes_str;
	}

	public void setWorktimes_str(String worktimes_str) {
		this.worktimes_str = worktimes_str;
	}

	public int getOaUserByBishopid() {
		return oaUserByBishopid;
	}

	public int getOaUserByAssistantid() {
		return oaUserByAssistantid;
	}

	public void setWorktimeid(int worktimeid) {
		this.worktimeid = worktimeid;
	}

	public int getSubjectid() {
		return subjectid;
	}

	public void setSubjectid(int subjectid) {
		this.subjectid = subjectid;
	}
}
